﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RiskMan.DataClass.Models;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.IO;
using System.Collections.ObjectModel;
using AvalonDock;
using RiskMan.DataClass.Models.Quik;
using RiskMan.DataClass.Models.RM;
using System.Windows;
using System.Reflection;

namespace RiskMan.Config
{
    public class XMLHistory
    {
        public static string FILENAME_QUIKITEMS = "\\QuikSettings.xml"; 
        public static string FILENAME_WINDOW = "\\Window.xml";


        public static object GetXmlData(string filename)
        {
            String path = Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments) + "\\OptionRobot";

            if (!Directory.Exists(path))
            {
                System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path);
                dir.Create();
            }

            if (filename == FILENAME_WINDOW)
            {
                try
                {
                    XmlSerializer reader = new XmlSerializer(typeof(List<string>));
                    StreamReader file = new StreamReader(path + filename);
                    List<string> result = new List<string>();
                    result = (List<string>)reader.Deserialize(file);
                    return result;
                }
                catch (Exception)
                {
                    return new List<string>();
                }
            }

        

            if (filename == FILENAME_QUIKITEMS)
            {
                try
                {
                    XmlSerializer reader = new XmlSerializer(typeof(QuikItems));
                    StreamReader file = new StreamReader(path + filename);
                    QuikItems result = new QuikItems();
                    result = (QuikItems)reader.Deserialize(file);
                    for (int i = 0; i < result.Count; i++)
                    {
                        result[i].IsConn = false;
                    }
                    return result;
                }
                catch (Exception ex)
                {
                    cfg.SetLog(ex.ToString(), new XMLHistory().GetType(), MethodBase.GetCurrentMethod());
                    return new QuikItems();
                }
            }
            

            return null;
        }

        public static void SetXmlData(string filename, object List)
        {
            String path = Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments) + "\\OptionRobot";

            if (!Directory.Exists(path))
            {
                System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path);
                dir.Create();
            }


            if (filename == FILENAME_WINDOW)
            {
                try
                {
                    XmlSerializer write = new System.Xml.Serialization.XmlSerializer(typeof(List<string>));
                    using (StreamWriter file = new System.IO.StreamWriter(path + filename))
                        write.Serialize(file, List);
                }
                catch (Exception ex)
                {
                    cfg.SetLog(ex.Message, new XMLHistory().GetType(), MethodBase.GetCurrentMethod());
                }
            }


           

            if (filename == FILENAME_QUIKITEMS)
            {
                try
                {
                    XmlSerializer write = new System.Xml.Serialization.XmlSerializer(typeof(QuikItems));
                    using (StreamWriter file = new System.IO.StreamWriter(path + filename))
                        write.Serialize(file, List);
                }
                catch (Exception ex)
                {
                    cfg.SetLog(ex.ToString(), new XMLHistory().GetType(), MethodBase.GetCurrentMethod());
                }
            }

           
        }
    }
}